//
//  EmailTypeViewController.m
//  cliqset
//
//  Created by David Hinson on 2/18/09.
//  Copyright 2009 Cliqset, Inc.. All rights reserved.
//

#import "EmailTypeViewController.h"
#import "cliqsetAppDelegate.h"

@implementation EmailTypeViewController

@synthesize emailTypes;
@synthesize parent;
@synthesize lastSelectedRow;

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	
	self.title = @"Label";
	
	self.emailTypes.backgroundColor = [UIColor clearColor];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
    // Release anything that's not essential, such as cached data
}

// Number of table sections
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
	return 1;
}

// Return rows in table
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	return [parent.valid count];
}	

// Draw cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
	UITableViewCell *cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:nil] autorelease];
    [cell backgroundView].opaque=YES;
    [cell backgroundView].clearsContextBeforeDrawing;
   	[cell setSelectionStyle:UITableViewCellSelectionStyleGray];
	
	UIFont* bold_large_font = [UIFont boldSystemFontOfSize:18];
	UIColor* textColor      = [UIColor colorWithRed:62.0/255.0 green:62.0/255.0 blue:62.0/255.0 alpha:1.0];
	
	cell.hidesAccessoryWhenEditing = NO;
	
	UILabel *label  = [[UILabel alloc] initWithFrame:CGRectMake(20, 12, 200, 20)];
	label.textColor = textColor;
	label.font      = bold_large_font;
	
	label.text = [parent.valid objectAtIndex:indexPath.row];
	if ([self.parent.emailTypeField isEqualToString:[parent.valid objectAtIndex:indexPath.row]]) {
		cell.accessoryType   = UITableViewCellAccessoryCheckmark;
		self.lastSelectedRow = indexPath.row;
	}
	
	[cell addSubview:label];
	[label release];
	
	//UIColor* myBGColor               = [UIColor colorWithRed:248.0/255.0 green:248.0/255.0 blue:248.0/255.0 alpha:1.0];
	//cell.backgroundView.backgroundColor = myBGColor;
	for ( UIView* view in cell.contentView.subviews ) 
	{
		view.backgroundColor = [ UIColor clearColor ];
	}
	
	return cell;
}

// Indicate disclosure
- (UITableViewCellAccessoryType)tableView:(UITableView *)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath
{
	if (self.lastSelectedRow == indexPath.row) {
		return UITableViewCellAccessoryCheckmark;	
	} 
	else {
		return UITableViewCellAccessoryNone;
	}
}

// Row selected... 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {	
	cliqsetAppDelegate* myApp = (cliqsetAppDelegate*)[[UIApplication sharedApplication] delegate];
	
	NSLog(@"didSelectRowAtIndexPath");
	
	lastSelectedRow = indexPath.row;
	
	UITableViewCell *hitCell = [tableView cellForRowAtIndexPath:indexPath];	
    [hitCell setSelected:NO animated:YES];
	
	if (indexPath.section == 0) {
		
		self.parent.emailTypeField = [parent.valid objectAtIndex:indexPath.row];
		
		[self.emailTypes reloadData];
		
		[myApp slideEmailTypeOut:parent];
	}
}

- (void)dealloc {
	[parent release];
	[emailTypes release];
    [super dealloc];
}


@end
